<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">入库作业单：</label>
                                        <div class="layui-input-inline">
                                            <input name="code" class="layui-input" type="text"
                                                   placeholder="入库作业单编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编号：</label>
                                        <div class="layui-input-inline">
                                            <input name="skuCode" class="layui-input" type="text"
                                                   placeholder="物料编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">入库仓：</label>
                                        <div class="layui-input-inline">
                                            <select name="warehouseId">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">入库类型：</label>
                                        <div class="layui-input-inline mr0">
                                            <select name="inoutTypeCode" lay-filter="inoutTypeCode">
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button type="button" class="layui-btn icon-btn color-green"
                                                lay-filter="formSubSearchLog" lay-submit>查询
                                        </button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button type="button" id="storageWorkAdd" class="layui-btn icon-btn color-reseda">
                                    添加入库作业单
                                </button>
                            </div>
                            <div class="lay-btn-rt">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="storageWorkTable" lay-filter="storageWorkTable"></table>
        </div>
    </div>
</div>

<style>
    .productStorageWorkDetailFormCalss .open-details-box {
        padding: 20px;
    }

    .productStorageWorkDetailFormCalss .open-details-box input {
        border: none !important;
        color: #666;
        width: auto;
    }

    .productStorageWorkDetailFormCalss .layui-form-label {
        width: 90px;
    }

    div[lay-id="workShowDetailTable"] .layui-table-cell,
    div[lay-id="workShowDetailTable"] .layui-form,
    div[lay-id="workShowDetailTable"] .layui-table-box,
    div[lay-id="workShowDetailTable"] .layui-table-body {
        overflow: visible;
    }

    .storageWorkIsSelect .layui-table-cell {
        height: auto;
    }
</style>

<script type="text/html" id="storageWorkTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="print">打印</a>-->
</script>

<!--料箱操作按钮-->
<script type="text/html" id="storageWorkBinListTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="add">添加入库信息</a>
</script>

<script type="text/html" id="workShowDetailTableBar">
    {{# if(d.storageCode != null){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="check">查询</a>
    {{# } }}
</script>


<!-- 添加入库作业弹窗 -->
<script type="text/html" id="storageWorkAddForm">
    <form lay-filter="storageWorkAddForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-row table-bor storageWorkIsSelect">
                    <table class="layui-table" id="workShowDetailTable" lay-filter="workShowDetailTable"></table>
                </div>
                <div class="layui-row table-bor scroll-table-4 workBinDom" style="margin-top: 15px;display: none">
                    <div class="layui-form-item">
                        <div class="open-details-title">料箱列表</div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label w-auto">输入料箱编码</label>
                        <div class="layui-input-inline" style="width: 200px;">
                            <input name="storageWorkBinCode" type="text" class="layui-input workBinCode"
                                   placeholder="请输入料箱编码"/>
                        </div>
                        <div class="layui-input-inline">
                            <a class="layui-btn icon-btn color-green" id="storageWorkBinListAdd">添加料箱</a>
                        </div>
                    </div>
                    <table class="layui-table" id="storageWorkBinListTable" lay-filter="storageWorkBinListTable"></table>
                </div>

            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>

<!--签收表格操作列-->
<script type="text/html" id="storageInfoListTableBar">
    {{# if(d.statusType=="created"){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="sign">签收</a>
    {{# }else if(d.statusType =="sign"){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    {{# } }}
</script>
<!--入库信息弹窗-->
<script type="text/html" id="storageInfoAddForm">
    <form lay-filter="storageInfoAddForm" class="layui-form model-form"
          style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;">
        <div class="open-details-box" style="height: calc(100% - 75px);">
            <div class="layui-row table-bor isSelectCode" style="margin-top: 15px;">
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">输入包装码</label>
                    <div class="layui-input-inline" style="width: 260px;">
                        <input name="workLabelCode" type="text" class="layui-input workBinCode"
                               placeholder="请输入包装码" style="width: 100%;"/>
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn icon-btn color-green" id="addWorkLabelCode">添加签收信息</button>
                    </div>
                </div>
                <div class="isSignLabelCode scroll-table-4" style="display: none">
                    <table class="layui-table" id="storageInfoListTable" lay-filter="storageInfoListTable"></table>
                </div>
            </div>
            <div class="layui-row isSignLabelCode" style="display: none;margin-top: 15px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">入库作业人</label>
                    <div class="layui-input-inline">
                        <input name="signStationName" placeholder="请输入入库作业人" type="text" class="layui-input"
                               readonly="readonly"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">作业说明</label>
                    <div class="layui-input-inline">
                        <input name="remark" placeholder="请输入入库作业说明" type="text" class="layui-input"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="storageInfoAddFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!--入库类型下拉框-->
<script type="text/html" id="storageTypeTemplate">
    <form lay-filter="storageTypeForm" class="layui-form model-form" style="padding: 0;">
        <select name="storageType" id="storageType" disabled lay-filter="storageType" lay-verify="required">
            <option value="">选择入库类型</option>
            <option value="production" selected="selected">生产入库</option>
            <option value="purchase">采购入库</option>
        </select>
    </form>
</script>

<!--入库签收单-->
<script type="text/html" id="storageCodeTemplate">
    <form lay-filter="storageCodeForm" class="layui-form model-form" style="padding: 0;">
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 240px;">
                <input name="storageCode" value="{{d.storageCode != null ? d.storageCode : ''}}" type="text"
                       class="layui-input" placeholder="请输入签收单号"/>
            </div>
            <div class="layui-input-inline" style="margin-left: 5px;">
                <a class="layui-btn icon-btn color-green" id="storageCodeConfig">确定</a>
            </div>
        </div>
    </form>
</script>

<!--物料编号-->
<script type="text/html" id="storageProductTemplate">
    <form lay-filter="storageProductForm" class="layui-form model-form" style="padding: 0;">
        <select name="storageProductSku" id="storageProductSku" lay-filter="storageProductSku" lay-verify="required">
            <option value="">请选择入库物料</option>
        </select>
    </form>
</script>

<!--选择仓库-->
<script type="text/html" id="warehouseTemplate">
    <form lay-filter="warehouseTemplateForm" class="layui-form model-form" style="padding: 0;">
        <select name="storageWarehouse" id="storageWarehouse" lay-filter="storageWarehouse" lay-verify="required">
        </select>
    </form>
</script>

<!--详情弹窗-->
<script type="text/html" id="productStorageWorkDetailForm">
    <form lay-filter="productStorageWorkDetailForm" class="layui-form open-form productStorageWorkDetailFormCalss">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <div class="open-details-title" style="font-size: 18px;">单据信息</div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库作业编号</label>
                        <div class="layui-input-inline">
                            <input name="code" type="text" class="layui-input text-red" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属入库计划</label>
                        <div class="layui-input-inline">
                            <input name="planCode" type="text" class="layui-input text-red" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">计划提交时间</label>
                        <div class="layui-input-inline">
                            <input name="planCreateTime" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库类型</label>
                        <div class="layui-input-inline">
                            <input name="storageTypeDesc" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">生产计划工单</label>
                        <div class="layui-input-inline">
                            <input name="productionPlanCode" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">生产台机工位</label>
                        <div class="layui-input-inline">
                            <input name="productionMachine" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">生产负责人</label>
                        <div class="layui-input-inline">
                            <input name="manufactureHeadName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">物料名称</label>
                        <div class="layui-input-inline">
                            <input name="productName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">送料数量</label>
                        <div class="layui-input-inline">
                            <input name="feedQuantity" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库单位</label>
                        <div class="layui-input-inline">
                            <input name="unitName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库包装方案</label>
                        <div class="layui-input-inline">
                            <input name="pageScheme" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库送料人</label>
                        <div class="layui-input-inline">
                            <input name="feedingPeople" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库仓库</label>
                        <div class="layui-input-inline">
                            <input name="warehouseName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">总签收数</label>
                        <div class="layui-input-inline">
                            <input name="signQuantity" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库签收人</label>
                        <div class="layui-input-inline">
                            <input name="signPeopleName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库签收工位</label>
                        <div class="layui-input-inline">
                            <input name="signStationName" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库签收时间</label>
                        <div class="layui-input-inline">
                            <input name="createTime" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs4">
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <input name="remark" type="text" class="layui-input" readonly="readonly"/>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="open-details-title" style="font-size: 18px;">签收包装码</div>
                    <table class="layui-table" id="productStorageSignDetailTable"
                           lay-filter="productStorageSignDetailTable"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var storageType = '', storageName = '', skuCode = ''; // 入库类型

        // 渲染表格
        var insTb = table.render({
            elem: '#storageWorkTable',
            url: config.base + 'enterprisecab/order/admin/storageWork/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                , {field: 'code', align: 'center', title: '入库作业单编号', width: 160}
                , {
                    field: 'planCode', align: 'center', title: '入库计划单编号', width: 160, templet(d) {
                        return d.inoutTypeCode == "production" ? d.planCode : d.purchaseCode;
                    }
                }
                , {field: 'skuCode', align: 'center', title: '物料编号', width: 160}
                , {field: 'productCode', align: 'center', title: '产品编号', width: 140}
                , {field: 'productType', align: 'center', title: '物料类型'}
                , {field: 'inoutTypeName', align: 'center', title: '入库类型'}
                , {field: 'unitName', align: 'center', title: '单位'}
                , {field: 'storageQuantity', align: 'center', title: '入库数量'}
                , {field: 'warehouseName', align: 'center', title: '入库仓库'}
                , {field: 'createTime', align: 'center', title: '入库时间', width: 160}
                , {field: 'statusDesc', align: 'center', title: '状态'}
                , {align: 'center', toolbar: '#storageWorkTableBar', title: '操作', width: 100}
            ]],
            done: function (res, curr, count) {
            }
        });

        //查询仓库
        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0) {
                    loadSelectHtml(res.data, "选择收货仓", "warehouseId", 2);
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        //查询入库类型
        admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/list', {inventoryType: 'in',limit:9999,status:'enable'}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0) {
                    loadSelectHtml(res.data, "选择入库类型", "inoutTypeCode", 6);
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        // 工具条点击事件
        table.on('tool(storageWorkTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                showDetailModel(data);
            }
        });

        //监听排序
        table.on('sort(storageWorkTable)', function (obj) {
            table.reload('storageWorkTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        //渲染下拉框
        form.render('select');

        // 显示详情弹窗
        function showDetailModel(data) {
            admin.open({
                type: 1,
                area: ['1500px', '90%'],
                title: '入库单详情',
                content: $('#productStorageWorkDetailForm').html(),
                success: function () {
                    let deliverProductArr = [];
                    // 查询入库商品信息
                    admin.req('enterprisecab/order/admin/storageWork/getStoragePackLabel', {jobId: data.id}, function (res) {
                        if (res.code == 200) {
                            deliverProductArr = res.data;
                            deliverProductArr.forEach((item, index) => {
                                deliverProductArr[index].storageTypeDesc = data.storageTypeDesc;
                            })

                            table.reload('productStorageSignDetailTable', {
                                data: deliverProductArr,
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    var storageWorkProductDetailTable = table.render({
                        elem: '#productStorageSignDetailTable',
                        data: deliverProductArr,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'packLabeCode', align: 'center', title: '包装SN编号', width: 220}
                            , {field: 'skuCode', align: 'center', title: '物料编号', width: 160}
                            , {field: 'skuSpecCode', align: 'center', title: '物料规格码', width: 160}
                            , {field: 'packName', align: 'center', title: '规格描述'}
                            , {field: 'storageTypeDesc', align: 'center', title: '入库类型'}
                            , {field: 'unitName', align: 'center', title: '最小单位'}
                            , {field: 'storageQuantity', align: 'center', title: '入库数量', width: 120}
                            , {field: 'createTime', align: 'center', title: '录入时间', width: 160}
                            , {field: 'statusDesc', align: 'center', title: '状态'}
                        ]]
                        , done: function (res, curr, count) {

                        }
                    });

                    form.val('productStorageWorkDetailForm', data);
                }
            });
        }

        // 添加入库作业单
        $("#storageWorkAdd").click(function () {
            storageWorkAddModel();
        });

        function storageWorkAddModel() {
            // storageType = '';
            storageType = $("select[name='storageType']").val();
            storageName = '';
            skuCode = '';
            admin.open({
                type: 1,
                area: ['82%', '90%'],
                title: '添加生产入库作业单',
                content: $('#storageWorkAddForm').html(),
                success: function () {
                    let planId = '', signId = '', warehouseId = '', storageCode = '', warehouseName = '',
                        signProductDetailId = ''; // 计划ID & 签收ID & 仓库ID
                    let detailData = [{}];
                    let productBoxList = [];
                    let signProductList = [];
                    // 详情列表
                    var insTb2 = table.render({
                        elem: '#workShowDetailTable',
                        data: detailData,
                        limit: detailData.limit,
                        cellMinWidth: 120,
                        page: false,
                        cols: [[
                            // {field: 'signCode', width: 180, title: '入库签收编号', templet: '<div><span class="distCode" style="display: block;cursor: pointer;" data-add="{{d.signCode != null ? false : true}}">{{d.signCode != null ? d.signCode : "请选择入库签收编号"}}</span></div>'}
                            {
                                field: 'storageType',
                                title: '入库类型',
                                templet: "#storageTypeTemplate",
                                width: 220,
                                align: 'center'
                            }
                            , {
                                field: 'storageCode',
                                title: '入库签收单',
                                templet: "#storageCodeTemplate",
                                width: 320,
                                align: 'center'
                            }
                            , {
                                field: 'storageProduct',
                                title: '物料编号',
                                templet: "#storageProductTemplate",
                                width: 260,
                                align: 'center'
                            }
                            , {field: 'productTypeName', title: '物料类型', align: 'center'}
                            , {field: 'unitName', title: '计价单位', align: 'center'}
                            , {field: 'storageQuantity', title: '入库数量', align: 'center'}
                            , {
                                field: 'production', title: '入库仓库', align: 'center', templet: function (d) {
                                    if (d.storageType == 'production') {
                                        return d.warehouseName
                                    }
                                }
                            }
                            , {
                                field: 'purchase',
                                title: '入库仓库',
                                templet: "#warehouseTemplate",
                                width: 220,
                                align: 'center'
                            }
                            , {
                                field: 'tableBar',
                                align: 'center',
                                toolbar: '#workShowDetailTableBar',
                                title: '操作',
                                width: 120
                            }
                        ]],
                        done: function () {
                            console.log(storageType);
                            $(".layui-table-box").find("[data-field='purchase']").css("display", "none");
                            $(".layui-table-box").find("[data-field='production']").css("display", "none");
                            if (storageType === 'production') {
                                $(".layui-table-box").find("[data-field='production']").css("display", "block");
                                $(".layui-table-box").find("[data-field='purchase']").css("display", "none");
                                $(".layui-table-body").find("[data-field='production']").css("height", "58.5px");
                                $(".layui-table-body").find("[data-field='production']").find(".layui-table-cell").css("lineHeight", "38px");
                            } else if (storageType === 'purchase') {
                                $(".layui-table-box").find("[data-field='production']").css("display", "none");
                                $(".layui-table-box").find("[data-field='purchase']").css("display", "block");
                                $(".layui-table-body").find("[data-field='purchase']").css("height", "58.5px");
                                $(".layui-table-body").find("[data-field='purchase']").find(".layui-table-cell").css("lineHeight", "38px");
                            }

                            // 确定入库签收单
                            $("#storageCodeConfig").click(function () {
                                storageCode = $("input[name='storageCode']").val();
                                if (storageType == '') {
                                    layer.msg('请选择入库类型', {icon: 2});
                                    return false;
                                }
                                if (storageCode == '') {
                                    layer.msg('请输入签收单号', {icon: 2});
                                    return false;
                                }
                                getDeliverySignInfo();
                            });
                        }
                    });

                    form.on('select(storageType)', function (d) {
                        storageType = d.value;
                        storageName = d.elem[d.elem.selectedIndex].text;
                        if (storageCode != d.value) {
                            $("input[name='storageCode']").val('');
                            loadSelectHtml([], '请选择入库物料', 'storageProductSku', 1);
                            $(".layui-table-body").find("[data-field='productTypeName']").find(".layui-table-cell").text('');
                            $(".layui-table-body").find("[data-field='unitName']").find(".layui-table-cell").text('');
                            $(".layui-table-body").find("[data-field='purchase']").find(".layui-table-cell").html('');
                            $(".layui-table-body").find("[data-field='tableBar']").find(".layui-table-cell").html('');
                            $(".layui-table-body").find("[data-field='storageQuantity']").find(".layui-table-cell").text('');
                            $(".layui-table-body").find("[data-field='production']").find(".layui-table-cell").text('');
                            // 清空料箱数据
                            productBoxList = []
                            $(".workBinDom").hide();
                        }
                    });

                    // 查询入库签收单下的物料
                    function getDeliverySignInfo() {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getDeliverySignInfo', {
                            code: storageCode,
                            storageType: storageType
                        }, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                signProductList = res.data.signProductList;
                                warehouseName = res.data.warehouseName;
                                warehouseId = res.data.warehouseId;
                                signId = res.data.id;
                                planId = res.data.planId;
                                loadSelectHtml(signProductList, '请选择入库物料', 'storageProductSku', 1)
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 选择物料
                    form.on('select(storageProductSku)', function (d) {
                        skuCode = d.elem[d.elem.selectedIndex].text;
                        signProductDetailId = d.value;
                        getSignPackLabelNum(signProductDetailId);
                    });

                    // 查询签收单物料信息和签收的条码总数
                    function getSignPackLabelNum(id) {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getSignPackLabelNum', {signProductDetailId: signProductDetailId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                let dataS = [{
                                    storageType: storageType,
                                    storageCode: storageCode,
                                    unitName: res.data.unitName,
                                    productTypeName: res.data.productTypeName,
                                    storageQuantity: res.data.storageQuantity,
                                    warehouseName: warehouseName,
                                }]
                                detailData = dataS;
                                table.reload('workShowDetailTable', {
                                    data: dataS,
                                    limit: dataS.length
                                });
                                // 重新赋值
                                loadSelectHtml(signProductList, '请选择入库物料', 'storageProductSku', 1, id);
                                // 入库类型的下拉框
                                $('#storageType option[value=' + storageType + ']').attr('selected', true);
                                // 如果是采购入库则查询仓库
                                if (storageType === 'purchase') {
                                    getWarehouseVOAll();
                                }
                                form.render('select');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 如果是采购入库则查询仓库
                    function getWarehouseVOAll() {
                        layer.load(2);
                        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                loadSelectHtml(res.data, '请选择仓库', 'storageWarehouse', 2)
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 选择仓库
                    form.on('select(storageWarehouse)', function (d) {
                        warehouseName = d.elem[d.elem.selectedIndex].text;
                        warehouseId = d.value;
                    });

                    // 工具条点击事件
                    table.on('tool(workShowDetailTable)', function (obj) {
                        var data = obj.data;
                        if (warehouseId == '') {
                            layer.msg('请选择仓库', {icon: 2});
                            return false;
                        }
                        if (obj.event === 'check') {
                            getPLanProductBoxByJobProductId();
                        }
                    });

                    // 查询物料下的料箱
                    function getPLanProductBoxByJobProductId() {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getPLanProductBoxByJobProductId', {
                            skuCode: skuCode,
                            warehouseId: warehouseId
                        }, function (res) {
                            layer.closeAll('loading');
                            $(".workBinDom").show();
                            if (res.code == 200) {
                                productBoxList = res.data;
                                productBoxList.forEach((item, index) => {
                                    productBoxList[index].storageQuantity = 0;
                                })
                                table.reload('storageWorkBinListTable', {
                                    data: productBoxList,
                                    limit: productBoxList.length
                                });

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 扫描料箱
                    $("#storageWorkBinListAdd").click(function () {
                        let code = $("input[name='storageWorkBinCode']").val();
                        if (code == '') {
                            layer.msg('请输入料箱编码', {icon: 2});
                            return false;
                        }
                        let filterList = productBoxList.filter((item) => {
                            return item.code == code;
                        });

                        if (filterList.length > 0) {
                            layer.msg('料箱编码已存在', {icon: 2});
                            return false;
                        }
                        getPalletBox(code);
                    });

                    // 扫描条码添加料箱
                    function getPalletBox(code) {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getPalletBox', {
                            code: code,
                            warehouseId: warehouseId
                        }, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                let resList = res.data;
                                resList.storageQuantity = 0;

                                productBoxList.push(resList);

                                // 渲染表格
                                table.reload('storageWorkBinListTable', {
                                    data: productBoxList,
                                    limit: productBoxList.length
                                });
                                $("input[name='storageWorkBinCode']").val('')
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 料箱列表
                    var storageWorkBinListTable = table.render({
                        elem: '#storageWorkBinListTable',
                        data: [],
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 50, type: 'numbers'}
                            , {field: 'code', title: '料箱编号', align: 'center'}
                            , {field: 'areaName', title: '所在库区', align: 'center'}
                            , {field: 'spaceCode', title: '所在货位', align: 'center'}
                            , {field: 'labelQuantity', title: '原始包装量', align: 'center'}
                            , {field: 'amountQuantity', title: '原始最小单位量', align: 'center'}
                            , {field: 'maximumLoad', title: '最大载重', align: 'center'}
                            , {field: 'storageQuantity', title: '新增最小单位量', align: 'center'}
                            , {field: 'updateTime', title: '最后变动时间', align: 'center'}
                            , {align: 'center', toolbar: '#storageWorkBinListTableBar', title: '操作'}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(storageWorkBinListTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'add') {
                            storageSignProductModel(data.id, signId, planId, data.code, warehouseId, signProductDetailId);
                        }
                    });

                    // 子页面刷新父页面
                    window.valueSignModel = function (valueSignMap) {
                        let signValue = 0;
                        valueSignMap.jobLabelList.forEach((item, index) => {
                            signValue += parseInt(item.storageQuantity);
                        });
                        productBoxList.forEach((item, index) => {
                            // 判断是否添加过条码
                            if (item.code == valueSignMap.boxCode) {
                                if (productBoxList[index].storageQuantity > 0) {
                                    productBoxList[index].storageQuantity = parseInt(signValue) + parseInt(item.storageQuantity);
                                } else {
                                    productBoxList[index].storageQuantity = signValue
                                }
                            }
                        });
                        table.reload('storageWorkBinListTable', {
                            data: productBoxList,
                            limit: productBoxList.length
                        });
                    }
                }
            });
        }

        // 入库签收物料信息
        function storageSignProductModel(boxId, signId, planId, boxCode, warehouseId, signProductDetailId) {
            var insTb3A = admin.open({
                type: 1,
                area: ['70%', '850px'],
                offset: '65px',
                title: '入库签收物料',
                content: $('#storageInfoAddForm').html(),
                success: function () {
                    let signLabelData = []; // 条码列表
                    $("input[name='signStationName']").val(config.getUser().username);

                    // 查询包装条码
                    $("#addWorkLabelCode").click(function () {
                        let code = $("input[name='workLabelCode']").val();
                        if (code == '') {
                            layer.msg('请输入包装码', {icon: 2});
                            return false;
                        }
                        let filterSign = signLabelData.filter((item) => {
                            return item.packLabeCode == code;
                        });

                        if (filterSign.length > 0) {
                            layer.msg('包装码已存在', {icon: 2});
                            return false;
                        }
                        getSignPackLabel(code);
                        return false;
                    });

                    // 扫描条码
                    function getSignPackLabel(code) {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getSignPackLabel', {
                            code: code,
                            signId: signId,
                            inoutType: '1',
                            signProductDetailId: signProductDetailId
                        }, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                $(".isSignLabelCode").show();
                                let napeData = res.data;
                                napeData.statusType = 'created';
                                napeData.warehouseId = warehouseId;
                                napeData.storageQuantity = napeData.packLabeQuantity;

                                signLabelData.push(napeData);
                                // 渲染表格
                                table.reload('storageInfoListTable', {
                                    data: signLabelData,
                                    limit: signLabelData.length
                                });
                                // 清空input
                                $("input[name='workLabelCode']").val('');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    var storageInfoListTable = table.render({
                        elem: '#storageInfoListTable',
                        data: signLabelData,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 50, type: 'numbers'}
                            , {field: 'packLabeCode', title: '包装编码', align: 'center'}
                            //,{field: 'planCode', title: '所属入库计划', align: 'center'}
                            , {field: 'productName', title: '物料名称', align: 'center'}
                            , {field: 'packName', title: '包装规格', align: 'center'}
                            , {field: 'quantity', title: '单位包装数', align: 'center'}
                            , {field: 'unitName', title: '最小单位', align: 'center'}
                            , {field: 'packLabeQuantity', title: 'SN码应收数', align: 'center'}
                            , {
                                field: 'storageQuantity', title: 'SN码签收数', align: 'center', templet: function (d) {
                                    if (d.statusType == 'sign') {
                                        return '<span>' + d.storageQuantity + '</span>'
                                    } else {
                                        return '<input signId="' + d.id + '" class="layui-input storageQuantity" style="height: 28px;" type="text" ' +
                                            'value="' + d.storageQuantity + '" />'
                                    }
                                }
                            }
                            , {align: 'center', toolbar: '#storageInfoListTableBar', title: '操作'}
                        ]]
                    });

                    // 监听签收数
                    $("body").delegate(".storageQuantity", "change", function () {
                        let id = $(this).attr('signId');
                        let reg = /^[1-9]+[0-9]*$/;
                        let storageQuantity = $(this).val();
                        signLabelData.forEach((item, index) => {
                            if ((item.id === id)) {
                                let quantity = parseInt(item.packLabeQuantity);
                                if (!reg.test(storageQuantity)) {
                                    layer.msg('请输入正确的数字', {icon: 2});
                                    storageQuantity = quantity;
                                    $(this).val(storageQuantity);
                                }
                                if (parseInt(storageQuantity) > quantity) {
                                    layer.msg('签收数量不能大于应收数量', {icon: 2});
                                    return false;
                                }
                            }
                        })
                    });

                    // 工具条点击事件
                    table.on('tool(storageInfoListTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'sign') { // 签收
                            let storageQuantity = $(this).parents('td').siblings().find('.storageQuantity').val(); //签收数
                            signLabelData.forEach((item, index) => {
                                if ((data.id === item.id)) {
                                    signLabelData[index].storageQuantity = storageQuantity;
                                    signLabelData[index].statusType = 'sign';
                                }
                            });
                        } else if (obj.event === 'edit') { //修改
                            signLabelData.forEach((item, index) => {
                                if ((data.id === item.id)) {
                                    signLabelData[index].statusType = 'created';
                                }
                            });
                        }
                        // 渲染表格
                        table.reload('storageInfoListTable', {
                            data: signLabelData,
                            limit: signLabelData.length
                        });
                    });

                    // 表单提交
                    form.on('submit(storageInfoAddFormSubmit)', function (d) {
                        if (signLabelData.length == 0) {
                            layer.msg('签收物资不能为空', {icon: 2});
                            return false;
                        }
                        let signMap = [];
                        signLabelData.forEach((item, index) => {
                            if (item.statusType != 'created') {
                                signMap.push({
                                    id: item.id,
                                    storageQuantity: item.storageQuantity,
                                    skuCode: item.skuCode,
                                    signProductDetailId: signProductDetailId,
                                })
                            }
                        });
                        if (signMap.length == 0) {
                            layer.msg('请先签收物资', {icon: 2});
                            return false;
                        }
                        let paramData = {
                            planId: planId,
                            remark: d.field.remark,
                            skuCode: skuCode,
                            signStationName: d.field.signStationName,
                            boxCode: boxCode,
                            inoutTypeName: storageName,
                            inoutTypeCode: "production",
                            jobLabelList: signMap
                        }
                        layer.load(2);

                        function doOk(data) {
                            //调用父页面方法
                            parent.valueSignModel(data);
                            layer.close(insTb3A);
                        }

                        admin.req('enterprisecab/order/admin/storageWork/addStorageWork', JSON.stringify(paramData), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('storageWorkTable');
                                doOk(paramData)
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }

        /*渲染下拉框*/
        function loadSelectHtml(data, tips, htmltarget, type, showValue) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].skuCode}</option>`;
                } else if (type === 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 6) {
                    html += `<option value='${data[i].code}'>${data[i].name}</option>`;

                }
            }

            $("form select[name='" + htmltarget + "']").html(html);

            if (showValue != '') {
                $('#' + htmltarget + ' option[value=' + showValue + ']').attr('selected', true)
            }

            form.render('select');
        }
    });
</script>